<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelAccounts extends JModel
{
	/**
	 * Customer accounts data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Customer accounts total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;

		$limit		= $mainframe->getUserStateFromRequest( $option.'.account.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.account.limitstart', 'limitstart', 0, 'int' );
		
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to get customer accounts data
	 *
	 * @access public
	 * @return array
	 */
	function getData() {

		// Lets load the customer accounts if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}

	/**
	 * Method to get the total number of customer accounts
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal() {
		// Lets load the customer accounts if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the customer accounts
	 *
	 * @access public
	 * @return object
	 */
	function getPagination() {

		// Lets load the customer account if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new FMCPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	/**
	 * Method to build the query for the accounts
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	*/
	function _buildQuery()
	{
		$user =& JFactory::getUser();
		$aid = $user->get('aid', 0);

		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		//Query to retrieve all customer accounts that belong under the web links section and that are published.
		$query = ' SELECT a.*, u.username FROM #__ownservice_accounts AS a '
				.' LEFT JOIN #__users AS u ON u.id=a.user_id '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
				.$where
				.$orderby;

		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the accounts
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy(){
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.account.filter_order', 'filter_order', 'a.account_no', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.account.filter_order_Dir', 'filter_order_Dir', '', 'cmd');

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;

		return $orderby;
	}

	/**
	 * Method to build the where clause of the query for the accounts
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		//Initialise variables
		$user				= & JFactory::getUser();
		$ownuser		= & Ownservice::getUser();
				
		//get vars
		$gid				= $user->gid;
		$level				= $ownuser->get('level_id');

		/*$filter_branch	= $mainframe->getUserStateFromRequest($option.'.account.filter_branch', 'filter_branch', 0, 'int');
		$filter_user		= $mainframe->getUserStateFromRequest($option.'.account.filter_user', 'filter_user', 0, 'int');
		$search 			= $mainframe->getUserStateFromRequest($option.'.account.fields.search', 'search', '', 'string' );
		$search 			= $this->_db->getEscaped( trim(JString::strtolower( $search ) ) );*/

		$sender				= JRequest::getVar('user_id', 0, 'post', 'int');
		$account_no		= JRequest::getVar('account_no', '', 'post');
		$account_name	= JRequest::getVar('account_name', '', 'post');

		$where = array();

		$where[] = 'a.deleted = 0';
		
		if($sender) {
			$where[] = 'a.user_id ='.(int) $sender;
		}

		if($account_no) {
			$where[] = 'LOWER( a.account_no ) ='.$this->_db->Quote($account_no);
		}

		if($account_name) {
			$where[] = 'LOWER( a.account_name ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $account_name, true ).'%', false );
		}
		/*if($filter_user){
			$where[] = 'a.user_id = '.$filter_user;
		}*/

		/*if($filter_branch) {
			$where[] = 'p.branch_id ='.$filter_branch;
		}*/

		/*if ($search) {
			$where[] = '(LOWER( a.account_name ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) .
				' OR LOWER( a.account_no ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) .
				' OR LOWER( a.bank_name ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) .
				' OR LOWER( a.branch_name ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ).
				' OR LOWER( p.firstname ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ).
				' OR LOWER( p.lastname ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ).
				' OR LOWER( u.username ) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ).')'
				;
		}*/

		/*if($gid < 25) {
			if(Ownservice::checkAccess('account','view','account','own')) {
				$where[] = 'a.user_id ='.$user->get('id');
			}

			if(Ownservice::checkAccess('account','view','account','all')) {
				if($level >1 && $level <5) {
					$where[] = 'p.branch_id ='.$ownuser->get('branch_id');
				}
			}
		}*/

		/*if($gid < 25) {
			$canView	= Ownservice::checkAccess('account','view','account','all');
			$canViewOwn = Ownservice::checkAccess('account','view','account','own');
		
			if($canViewOwn){
				$where[] = ' a.user_id='.$user->get('id');
			}

			if($canView) {
				if($level > 1 && $level < 5) {
					$where[] = ' p.branch_id ='.$ownuser->get('branch_id');
				}
			}
		}*/

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;

	}

	/**
	 * Method to remove a customer account
	 *
	 * @access public
	 * @return boolean true on success
	*/
	function delete($id){

		$user = & JFactory::getUser();

		if($user->gid < 25) {

			$canDeleteOwn	= Ownservice::checkAccess('account', 'delete', 'account', 'own');

			if($canDeleteOwn) {
				$where = ' AND user_id='.$user->get('id');
			}
		}

		$query = " UPDATE #__ownservice_accounts SET deleted=1 WHERE id=".$id.$where;

		$this->_db->setQuery($query);
		if(!$this->_db->query()){
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}


	/**
	 * Method to get users
	 *
	 * @access public
	 * @return object
	*/
	function getUsers() {

		//Initialise variables
		$user =& JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		// get vars
		$gid		= $user->gid;
		$level		= $ownuser->get('level_id');

		$where = array();
		$where[] = 'u.block=0';
		$where[] = 'p.group_id = 1';
		
		/*if(Ownservice::checkAccess('account','add','account','own')){
			$where[] = ' u.id='.$user->get('id');
		}*/

		if($gid < 25) {
			$canView	= Ownservice::checkAccess('account','view','account','all');
			$canViewOwn = Ownservice::checkAccess('account','view','account','own');
		
			if($canViewOwn){
				$where[] = ' u.id='.$user->get('id');
			} 

			if($canView) {
				if($level > 1 && $level < 5) {
					$where[] = ' p.branch_id ='.$ownuser->get('branch_id');
				}
			}
		}
	
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$query = 'SELECT u.id, u.name '
				.' FROM #__users AS u '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id=u.id '
				.' LEFT JOIN #__ownservice_groups AS g ON g.id=p.group_id '
				.$where
				.' ORDER BY u.name';

		$this->_db->setQuery($query);
		$users = $this->_db->loadObjectList();

		return $users;
	}

	/**
	 * Method to get branches
	 *
	 * @return object
	 * @since 1.5
	*/
	function getBranches() {
		$query = ' SELECT * FROM #__ownservice_branches  '
				.' WHERE deleted=0 AND published = 1'
				.' ORDER BY name';

		$this->_db->setQuery($query);
		$branches = $this->_db->loadObjectList();

		return $branches;
	}

	/**
	 * Method get user
	 *
	 * @return object
	 * @since 1.5
	*/
	function getUser($id) {

		$query = ' SELECT p.*, u.name FROM #__ownservice_users AS p '
				.' LEFT JOIN #__users AS u ON u.id=p.user_id '
				.' WHERE u.id='.$id.' AND u.block = 0 ';
		$this->_db->setQuery($query);
		$profile = $this->_db->loadObject();

		return $profile;
	}
}